#include <stdio.h>
#include <stdlib.h>


typedef struct linknode
{
    int data;
    struct linknode *next;
}link_node;

link_node *creat()                                                                                                                                                                                                                                                              
{
    link_node *H=(link_node *)malloc(sizeof(link_node));
    H->data=0;
    H->next=NULL;

    return H;
}

int insert(link_node *H,int mydata)
{
    link_node *p=(link_node *)malloc(sizeof(link_node));
    link_node *q=H;
    while(H->next!=NULL)
    {
        H=H->next;
    }
    p->next=NULL;
    H->next=p;
    p->data=mydata;
    q->data++;

    return 0;
}

int view(link_node *H)
{
    link_node *p=H;
    while(p->next!=NULL)
    {
        p=p->next;
        printf("%d ",p->data);

    }
    putchar(10);

    return 0;
}

int linkcycle(link_node *H)
{
	link_node *p=H->next;
	link_node *q=H->next;
	int num=0;
	while(p!=q||num==0)
	{
		p=p->next;
		q=q->next;
		q=q->next;
		printf("p->data:%d q->data:%d \n",p->data,q->data);
		num++;
	}
	printf("youhuan:num= %d\n",num);
	int pos=0;
	link_node *a=H->next;
	while(a!=p)
	{
		a=a->next;
		p=p->next;
		pos++;
	}
	return pos;
}


int main(int argc, const char *argv[])
{
	link_node *H=creat();
	insert(H,3);
	link_node *a1=H->next;
	printf("a1=%d ",a1->data);
	insert(H,2);
	link_node *a2=a1->next;
	printf("a2=%d ",a2->data);

	insert(H,0);
	link_node *a3=a2->next;
	printf("a3=%d ",a3->data);

	insert(H,4);
	link_node *a4=a3->next;
	printf("a4=%d ",a4->data);
	a4->next=a2;
	printf("a4->next->data:%d ",a4->next->data);

	putchar(10);

	int pos=linkcycle(H);
	printf("pos = %d\n",pos);

	return 0;
}
